<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

namespace app\admin\model;

use think\Model;
use think\Db;

/**
 * 产品
 * @package app\admin\model
 */
class ProductModel extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'e_product';
	
	/**
     * 获取产品列表
     * @param int $page 当前分页
	 * @param int $export 是否导出
     * @param string $keyword 搜索关键字
     * @author 郑义生 <1052063941@qq.com>
     * @return mixed
     */
	public function getProductList($input)
	{
		$query = Db::name($this->table)->where('product_status','in','[0,1]');
		$page = isset($input['page']) ? $input['page'] : 1;
		$export = isset($input['export']) ? $input['export'] : 0;
		$order_field = isset($input['order_field']) ? $input['order_field'] : '';
		$order_value = isset($input['order_value']) ? $input['order_value'] : 'desc';
		if(isset($input['keyword']) && !empty($input['keyword'])){
			$query = $this->where('product_code|product_name','like','%'.$input['keyword'].'%');
		}
		if(isset($input['type_id']) && !empty($input['type_id'])){
			$query = $this->where('type_id',$input['type_id']);
		}
		if(isset($input['product_sale']) && !empty($input['product_sale'])){
			$query = $this->where('product_sale',$input['product_sale']);
		}
		$order_arr = ['id'=>'desc'];
		if(!empty($order_field)){
			$order_arr = [$order_field=>$order_value];
		}
		if($export){
			$list = $query->order($order_arr)->select();
			return [
				'list'=>$list,
				'page'=>array()
			];
		}else{
			$list = $query->order($order_arr)->paginate(config('paginate.list_rows'));
			$page = $list->render();
			return [
				'list'=>$list->toArray(),
				'page'=>$page
			];
		}
	}
	
	/**
     * 获取产品详情
     * @param int $id 产品ID
     * @author 郑义生 <1052063941@qq.com>
     * @return mixed
     */
	public function getProductView($input)
	{
		$product_id = $input['id'];
		$product['base'] = Db::name($this->table)->where('id',$product_id)->find();
		$product['content'] = Db::name('e_product_content')->where('product_id',$product_id)->find();
		$product['files'] = Db::name('e_product_file')->where(array('product_id'=>$product_id,'file_status'=>0))->select();
		$product['sizes'] = Db::name('e_product_size')->where('product_id',$product_id)->find();
		$product['exchange'] = Db::name('e_product_exchange')->where(array('product_id'=>$product_id,'fact_status'=>0))->select();
		$product['mator'] = Db::name('e_product_sku')->where(array('product_id'=>$product_id,'sku_status'=>0))->select();
		return $product;
	}
	
	/**
     * 添加、更新产品数据
     * @param array $input 数组
     * @author 郑义生 <1052063941@qq.com>
     * @return mixed
     */
	public function delData($input)
	{
		$product_id = $input['id'];
		Db::startTrans();
		try{
			Db::table('e_product')->where('id', $product_id)->update(['product_status'=>-1]);
			Db::table('e_product_file')->where('product_id', $product_id)->update(['file_status'=>-1]);
			Db::table('e_product_size')->where('product_id', $product_id)->update(['rela_status'=>-1]);
			Db::table('e_product_sku')->where('product_id', $product_id)->update(['sku_status'=>-1]);
			Db::table('e_product_exchange')->where('product_id', $product_id)->update(['fact_status'=>-1]);
			Db::commit();    
			return true; 
		} catch (\Exception $e) {
			Db::rollback();
			return false;
		}
	}
	
	/**
     * 添加、更新产品数据
     * @param array $input 数组
     * @author 郑义生 <1052063941@qq.com>
     * @return mixed
     */
	public function insertUpdateData($input)
	{
		$product_id = $input['id'];
		$data = array();
		$data_content = array();
		$data_files = array();
		$data_sizes = array();
		$data_exchange = array();
		$data_mator = array();
		if(isset($input['product_code'])){
			$data['product_code'] = $input['product_code'];
		}
		if(isset($input['product_name'])){
			$data['product_name'] = $input['product_name'];
		}
		if(isset($input['type_id'])){
			$data['type_id'] = $input['type_id'];
		}
		if(isset($input['product_sale'])){
			$data['product_sale'] = $input['product_sale'];
		}
		if(isset($input['product_dw'])){
			$data['product_dw'] = $input['product_dw'];
		}
		if(isset($input['product_status'])){
			$data['product_status'] = $input['product_status'];
		}
		/*
		if(isset($input['product_zx'])){
			$data_content['product_zx'] = $input['product_zx'];
		}
		if(isset($input['product_gl'])){
			$data_content['product_gl'] = $input['product_gl'];
		}
		if(isset($input['product_yc'])){
			$data_content['product_yc'] = $input['product_yc'];
		}
		if(isset($input['product_style'])){
			$data_content['product_style'] = $input['product_style'];
		}
		*/
		if(isset($input['product_cg'])){
			$data_content['product_cg'] = $input['product_cg'];
		}
		if(isset($input['product_demo'])){
			$data_content['product_demo'] = $input['product_demo'];
		}
		$file_info = isset($input['file_info']) ? $input['file_info'] : array();
		$size_info = isset($input['size_info']) ? $input['size_info'] : array();
		$fact_info = isset($input['fact_info']) ? $input['fact_info'] : array();
		$mator_ids = isset($input['mator_ids']) ? $input['mator_ids'] : array();
		$product_params = isset($input['product_params']) ? $input['product_params'] : array();
		if(!empty($product_params)){
			$product_params_data = array();
			foreach($product_params as $_p){
				$p_arr = explode('-',$_p);
				array_push(
					$product_params_data,
					array('title'=>$p_arr[0],'content'=>$p_arr[1])
				);
			}
			$data_content['product_params'] = serialize($product_params_data);
		}
		if($product_id == 0){
			if(!empty($file_info)){
				foreach($file_info as $file){
					$file_arr = explode('-',$file);
					array_push(
						$data_files,
						array(
							'file_name'=>$file_arr[1],
							'file_url'=>$file_arr[2],
							'file_sort'=>$file_arr[3],
							'file_type'=>$file_arr[4],
							'create_time'=>time(),
							'update_time'=>time()
						)
					);
				}
			}
			if(!empty($size_info)){
				foreach($size_info as $size){
					$size_arr = explode('-',$size);
					$data_sizes[$size_arr[0]] = $size_arr[1];
				}
				$data_sizes['create_time'] = time();
				$data_sizes['update_time'] = time();
			}
			if(!empty($fact_info)){
				foreach($fact_info as $fact){
					$fact_arr = explode('-',$fact);
					array_push(
						$data_exchange,
						array(
							'exchange_code'=>$fact_arr[0],
							'fact_code'=>$fact_arr[1],
							'fact_name'=>$fact_arr[2],
							'fact_type'=>$fact_arr[3],
							'fact_demo'=>$fact_arr[4],
							'create_time'=>time(),
							'update_time'=>time()
						)
					);
				}
			}
			if(!empty($mator_ids)){
				foreach($mator_ids as $mator){
					array_push(
						$data_mator,
						array(
							'mator_id'=>$mator,
							'create_time'=>time(),
							'update_time'=>time()
						)
					);
				}
			}
		}
		Db::startTrans();
		try{
			if($product_id > 0){
				$data['update_time'] = time();
				Db::table('e_product')->where('id', $product_id)->update($data);
				Db::table('e_product_content')->where('product_id', $product_id)->update($data_content);
				if(!empty($file_info)){
					foreach($file_info as $file){
						$update_files = array();
						$file_arr = explode('-',$file);
						$file_id = $file_arr[0];
						if($file_id > 0){
							$update_files['file_name'] = $file_arr[1];
							$update_files['file_url'] = $file_arr[2];
							$update_files['file_sort'] = $file_arr[3];
							$update_files['update_time'] = time();
							Db::table('e_product_file')->where('id', $file_id)->update($update_files);
						}else{
							array_push(
								$data_files,
								array(
									'product_id'=>$product_id,
									'file_name'=>$file_arr[1],
									'file_url'=>$file_arr[2],
									'file_sort'=>$file_arr[3],
									'file_type'=>$file_arr[4],
									'create_time'=>time(),
									'update_time'=>time()
								)
							);
						}
					}
					if(!empty($data_files)){
						Db::name('e_product_file')->insertAll($data_files);
					}
				}
				if(!empty($size_info)){
					$update_sizes = array();
					foreach($size_info as $size){
						$size_arr = explode('-',$size);
						$update_sizes[$size_arr[0]] = $size_arr[1];
						$update_sizes['update_time'] = time();
					}
					Db::table('e_product_size')->where('product_id', $product_id)->update($update_sizes);
				}
				if(!empty($fact_info)){
					$fact_row = Db::name('e_product_exchange')->where(array('product_id'=>$product_id,'fact_status'=>0))->select();
					$code_arr = array();
					if(!empty($fact_row)){
						foreach($fact_row as $_f){
							array_push($code_arr,$_f['exchange_code']);
						}
					}
					$new_code_arr = array();
					foreach($fact_info as $fact){
						$fact_arr = explode('-',$fact);
						array_push($new_code_arr,$fact_arr[0]);
					}
					$last_code = array_diff($new_code_arr,$code_arr);
					if(!empty($last_code)){
						foreach($fact_info as $fact){
							$fact_arr = explode('-',$fact);
							if(in_array($fact_arr[0],$last_code)){
								array_push(
									$data_exchange,
									array(
										'product_id'=>$product_id,
										'exchange_code'=>$fact_arr[0],
										'fact_code'=>$fact_arr[1],
										'fact_name'=>$fact_arr[2],
										'fact_type'=>$fact_arr[3],
										'fact_demo'=>$fact_arr[4],
										'create_time'=>time(),
										'update_time'=>time()
									)
								);
							}
						}
						if(!empty($data_exchange)){
							Db::name('e_product_exchange')->insertAll($data_exchange);
						}
					}
				}
				if(!empty($input['del_fact_codes'])){
					Db::table('e_product_exchange')->where('product_id', $product_id)->where('exchange_code','in',$input['del_fact_codes'])->update(['fact_status'=>-1,'update_time'=>time()]);
				}
				if(!empty($mator_ids)){
					$mator_row = Db::name('e_product_sku')->where(array('product_id'=>$product_id,'sku_status'=>0))->select();
					$id_arr = array();
					if(!empty($mator_row)){
						foreach($mator_row as $_m){
							array_push($id_arr,$_m['mator_id']);
						}
					}
					$new_id_arr = array();
					foreach($mator_ids as $mator){
						array_push($new_id_arr,$mator);
					}
					$last_id = array_diff($new_id_arr,$id_arr);
					if(!empty($last_id)){
						foreach($mator_ids as $mator){
							if(in_array($mator,$last_id)){
								array_push(
									$data_mator,
									array(
										'product_id'=>$product_id,
										'mator_id'=>$mator,
										'create_time'=>time(),
										'update_time'=>time()
									)
								);
							}
						}
						if(!empty($data_mator)){
							Db::name('e_product_sku')->insertAll($data_mator);
						}
					}
				}
				if(!empty($input['del_mator_ids'])){
					Db::table('e_product_sku')->where('product_id', $product_id)->where('mator_id','in',$input['del_mator_ids'])->update(['sku_status'=>-1,'update_time'=>time()]);
				}
			}else{
				$data['create_time'] = time();
				$data['update_time'] = time();
				Db::name('e_product')->insert($data);
				$product_id = Db::name('e_product')->getLastInsID();
				if(!empty($data_content)){
					$data_content['product_id'] = $product_id;
					$data_content['create_time'] = time();
					$data_content['update_time'] = time();
					Db::name('e_product_content')->insert($data_content);
				}
				if(!empty($data_files)){
					foreach($data_files as $_f_k=>$_f_v){
						$data_files[$_f_k]['product_id'] = $product_id;
					}
					Db::name('e_product_file')->insertAll($data_files);
				}
				if(!empty($data_sizes)){
					$data_sizes['product_id'] = $product_id;
					Db::name('e_product_size')->insert($data_sizes);
				}
				if(!empty($data_exchange)){
					foreach($data_exchange as $_e_k=>$_e_v){
						$data_exchange[$_e_k]['product_id'] = $product_id;
					}
					Db::name('e_product_exchange')->insertAll($data_exchange);
				}
				if(!empty($data_mator)){
					foreach($data_mator as $_m_k=>$_m_v){
						$data_mator[$_m_k]['product_id'] = $product_id;
					}
					Db::name('e_product_sku')->insertAll($data_mator);
				}
			}
			Db::commit();    
			return true; 
		} catch (\Exception $e) {
			Db::rollback();
			return false;
		}
	}
	/*
     * 导入产品互换编码数据
     * @param array $input 数组
     * @author 郑义生 <1052063941@qq.com>
     * @return mixed
     */
	public function productImport($data)
	{
		$product_codes = array();
		$type_codes = array();
		foreach($data as $_d){
			array_push($product_codes,$_d['product_code']);
			array_push($type_codes,$_d['type_code']);
		}
		$product = Db::name('e_product')->where('product_code','in',$product_codes)->select();
		foreach($product as $_p){
			$product_codes[$_p['product_code']] = $_p['id'];
		}
		$types = Db::name('e_product_type')->where('type_code','in',$type_codes)->select();
		foreach($types as $_t){
			$type_codes[$_t['type_code']] = $_t['id'];
		}
		foreach($data as $_d){
			$insert_data = array();
			$insert_extends_data = array();
			$product = Db::name('e_product')->where(array('product_code'=>$_d['product_code']))->find();
			if(!empty($product)){
				continue;
			}
			$insert_data['product_code'] = $_d['product_code'];
			$insert_data['product_name'] = $_d['product_name'];
			$insert_data['type_id'] = $type_codes[$_d['type_code']];
			$insert_data['product_sale'] = $_d['product_sale'];
			$insert_data['product_dw'] = $_d['product_dw'];
			$insert_data['product_status'] = 1;
			$insert_data['create_time'] = time();
			$insert_data['update_time'] = time();
			$insert_extends_data['product_cg'] = $_d['product_cg'];
			$insert_extends_data['product_demo'] = $_d['product_demo'];
			$insert_extends_data['create_time'] = time();
			$insert_extends_data['update_time'] = time();
			Db::name('e_product')->insert($insert_data);
			$insert_extends_data['product_id'] = Db::name('e_product')->getLastInsID();
			Db::name('e_product_content')->insert($insert_extends_data);
		}
		return true;
	}
	
	/*
     * 导入产品尺寸数据
     * @param array $input 数组
     * @author 郑义生 <1052063941@qq.com>
     * @return mixed
     */
	public function productSizeImport($data)
	{
		$product_codes = array();
		foreach($data as $_d){
			array_push($product_codes,$_d['product_code']);
		}
		$product = Db::name('e_product')->where('product_code','in',$product_codes)->select();
		foreach($product as $_p){
			$product_codes[$_p['product_code']] = $_p['id'];
		}
		$insert_data = array();
		foreach($data as $_d){
			$product_code = $_d['product_code'];
			unset($_d['product_code']);
			$product = Db::name('e_product_size')->where(array('product_id'=>$product_codes[$product_code]))->find();
			$_d['update_time'] = time();
			if(!empty($product)){
				Db::name('e_product_size')->where(array('product_id'=>$product_codes[$product_code]))->update($_d);
			}else{
				$_d['product_id'] = $product_codes[$product_code];
				$_d['create_time'] = time();
				array_push($insert_data,$_d);
			}
		}
		if(!empty($insert_data)){
			return Db::name('e_product_size')->insertAll($insert_data);
		}
		return true;
	}
	
	/*
     * 导入产品互换编码数据
     * @param array $input 数组
     * @author 郑义生 <1052063941@qq.com>
     * @return mixed
     */
	public function productContentImport($data)
	{
		$product_codes = array();
		$product_extends = array();
		foreach($data as $_d){
			array_push($product_codes,$_d['product_code']);
			$product_extends[$_d['product_code']][] = array('title'=>$_d['product_title'],'content'=>$_d['product_content']);
		}
		$product = Db::name('e_product')->where('product_code','in',$product_codes)->select();
		foreach($product as $_p){
			$product_codes[$_p['product_code']] = $_p['id'];
		}
		$insert_data = array();
		foreach($data as $_d){
			$product = Db::name('e_product_content')->where(array('product_id'=>$product_codes[$_d['product_code']]))->find();
			if(!empty($product)){
				$update_data = array();
				$update_data['product_params'] = serialize($product_extends[$_d['product_code']]);
				$update_data['update_time'] = time();
				Db::name('e_product_content')->where(array('product_id'=>$product_codes[$_d['product_code']]))->update($update_data);
			}else{
				array_push(
					$insert_data,
					array(
						'product_id'=>$product_codes[$_d['product_code']],
						'product_params'=>serialize($product_extends[$_d['product_code']]),
						'create_time'=>time(),
						'update_time'=>time(),
					)
				);
			}
		}
		if(!empty($insert_data)){
			return Db::name('e_product_content')->insertAll($insert_data);
		}
		return true;
	}
	/*
     * 导入产品互换编码数据
     * @param array $input 数组
     * @author 郑义生 <1052063941@qq.com>
     * @return mixed
     */
	public function productChangeImport($data)
	{
		$product_codes = array();
		foreach($data as $_d){
			array_push($product_codes,$_d['product_code']);
		}
		$product = Db::name('e_product')->where('product_code','in',$product_codes)->select();
		foreach($product as $_p){
			$product_codes[$_p['product_code']] = $_p['id'];
		}
		$insert_data = array();
		foreach($data as $_d){
			$change = Db::name('e_product_exchange')->where(array('product_id'=>$product_codes[$_d['product_code']],'exchange_code'=>$_d['exchange_code'],'fact_code'=>$_d['fact_code']))->find();
			if(!empty($change)){
				continue;
			}
			array_push(
				$insert_data,
				array(
					'product_id'=>$product_codes[$_d['product_code']],
					'exchange_code'=>$_d['exchange_code'],
					'fact_code'=>$_d['fact_code'],
					'fact_name'=>$_d['fact_name'],
					'fact_type'=>$_d['fact_type'],
					'fact_demo'=>$_d['fact_demo'],
					'create_time'=>time(),
					'update_time'=>time(),
				)
			);
		}
		if(!empty($insert_data)){
			return Db::name('e_product_exchange')->insertAll($insert_data);
		}
		return true;
	}
	
	/*
     * 导入产品车型关联数据
     * @param array $input 数组
     * @author 郑义生 <1052063941@qq.com>
     * @return mixed
     */
	public function productMatorImport($data)
	{
		$product_codes = array();
		$mator_codes = array();
		foreach($data as $_d){
			array_push($product_codes,$_d['product_code']);
			array_push($mator_codes,$_d['mator_code']);
		}
		$product = Db::name('e_product')->where('product_code','in',$product_codes)->select();
		foreach($product as $_p){
			$product_codes[$_p['product_code']] = $_p['id'];
		}
		$mator = Db::name('e_mator')->where('mator_code','in',$mator_codes)->select();
		foreach($mator as $_m){
			$mator_codes[$_m['mator_code']] = $_m['id'];
		}
		$insert_data = array();
		foreach($data as $_d){
			$mator = Db::name('e_product_sku')->where(array('product_id'=>$product_codes[$_d['product_code']],'mator_id'=>$mator_codes[$_d['mator_code']]))->find();
			if(!empty($mator)){
				continue;
			}
			array_push(
				$insert_data,
				array(
					'product_id'=>$product_codes[$_d['product_code']],
					'mator_id'=>$mator_codes[$_d['mator_code']],
					'create_time'=>time(),
					'update_time'=>time(),
				)
			);
		}
		if(!empty($insert_data)){
			return Db::name('e_product_sku')->insertAll($insert_data);
		}
		return true;
	}
	
	/*
     * 导入产品附件数据
     * @param array $input 数组
     * @author 郑义生 <1052063941@qq.com>
     * @return mixed
     */
	public function productFileImport($data)
	{
		$product_codes = array();
		foreach($data as $_d){
			array_push($product_codes,$_d['product_code']);
		}
		$product = Db::name('e_product')->where('product_code','in',$product_codes)->select();
		foreach($product as $_p){
			$product_codes[$_p['product_code']] = $_p['id'];
		}
		$insert_data = array();
		foreach($data as $_d){
			array_push(
				$insert_data,
				array(
					'product_id'=>$product_codes[$_d['product_code']],
					'file_type'=>$_d['file_type'],
					'file_name'=>$_d['file_name'],
					'file_url'=>$_d['file_url'],
					'create_time'=>time(),
					'update_time'=>time(),
				)
			);
		}
		return Db::name('e_product_file')->insertAll($insert_data);
	}
	
	/*
     * 导入产品图片数据
     * @param array $input 数组
     * @author 郑义生 <1052063941@qq.com>
     * @return mixed
     */
	public function productImagesImport($data)
	{
		$product_codes = array();
		foreach($data as $_d){
			array_push($product_codes,$_d['product_code']);
		}
		$product = Db::name('e_product')->where('product_code','in',$product_codes)->select();
		foreach($product as $_p){
			$product_codes[$_p['product_code']] = $_p['id'];
		}
		$insert_data = array();
		foreach($data as $_d){
			array_push(
				$insert_data,
				array(
					'product_id'=>$product_codes[$_d['product_code']],
					'file_name'=>$_d['file_name'],
					'file_url'=>$_d['file_url'],
					'create_time'=>time(),
					'update_time'=>time(),
				)
			);
		}
		return Db::name('e_product_file')->insertAll($insert_data);
	}	
}
